package com.mango.ch03;

import java.io.IOException;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import com.mango.HDFSTools.HDFSAPI;

/**
 * 使用传统的方法来求得topN
 * 
 * @author Mango
 *
 */
public class TopN_CatDemoJob {
	public static Path INPUT_PATH = new Path("/TopN_CatDemoJob/input");
	public static Path OUTPUT_PATH = new Path("/TopN_CatDemoJob/output");

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		// 创建输入文件夹
		try {
			HDFSAPI hdfs = new HDFSAPI();
			hdfs.createDirectory(INPUT_PATH);
			// 先删除已经有的输出文件夹
			hdfs.delDirectory(OUTPUT_PATH);
			// hdfs.orpOver();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			System.out.println("----------文件操作失败");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Configuration conf = new Configuration();
		conf.set("top.n", "10");
		Job job = Job.getInstance(conf, "top10CatDemo");

		job.setJarByClass(TopN_CatDemoJob.class);

		// mapper & reducer
		job.setMapperClass(TopN_CatDemoMapper.class);
		job.setReducerClass(TopN_CatDemoReducer.class);
		// setting tasks num
		job.setNumReduceTasks(2);
		// set map & reducer input-output data type
		job.setMapOutputKeyClass(NullWritable.class);
		job.setMapOutputValueClass(Text.class);
		job.setOutputKeyClass(NullWritable.class);
		job.setOutputValueClass(Text.class);

		FileInputFormat.setInputPaths(job, INPUT_PATH);
		FileOutputFormat.setOutputPath(job, OUTPUT_PATH);
		boolean status;
		try {
			status = job.waitForCompletion(true);
			System.exit(status ? 0 : 1);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("a      errror");
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			System.out.println("b      errror");
			e.printStackTrace();
		}

	}

}
